package com.wmh.baseservice.skd.controller;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.wmh.baseservice.common.service.TokenService;
import com.wmh.baseservice.common.utils.returnbody.ReturnBody;
import com.wmh.baseservice.skd.pojo.SkdGroupBuyingActivities;
import com.wmh.baseservice.skd.pojo.SkdShoppingCart;
import com.wmh.baseservice.skd.service.SkdGroupBuyingActivitiesService;
import com.wmh.baseservice.skd.service.SkdShoppingCartService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import lombok.extern.slf4j.Slf4j;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;
import javax.validation.constraints.NotNull;
import java.util.ArrayList;
import java.util.List;
import java.util.Objects;

@Api(tags = "团购商品订单相关接口")
@Validated
@RestController
@RequestMapping(value = "shopping-cart", produces = "text/plain;charset=utf-8")
@Slf4j
public class ShoppingCartController {
    @Resource
    private TokenService tokenService;
    @Resource
    private SkdShoppingCartService skdShoppingCartService;
    @Resource
    private SkdGroupBuyingActivitiesService skdGroupBuyingActivitiesService;

    /**
     * #0058 添加商品到购物车
     * @param groupBuyingActivityId 团购活动id
     * @param goodsId 商品id
     * @param goodsSpecId 商品规格id
     * @return 返回是否添加成功信息
     */
    @ApiOperation("添加商品到购物车")
    @PostMapping
    public String addShoppingCart(@NotNull(message = "未提交团购活动id") Long groupBuyingActivityId,
                                  @NotNull(message = "未提交商品id") Long goodsId,
                                  Long goodsSpecId) {
        return skdShoppingCartService.addShoppingCart(groupBuyingActivityId, goodsId, goodsSpecId);
    }

    /**
     * #0059 增加购物车商品数量
     * @param shoppingCartId 购物车商品id
     * @return 返回是否增加成功信息
     */
    @ApiOperation("增加购物车商品数量")
    @PostMapping("add-quantity")
    public String addQuantity(@NotNull(message = "未提交购物车商品id") Long shoppingCartId) {
        // 增加购物车商品数量, 将quantity字段值每次加一
        SkdShoppingCart shoppingCart = skdShoppingCartService.getById(shoppingCartId);
        if (!Objects.isNull(shoppingCart)) {
            shoppingCart.setQuantity(shoppingCart.getQuantity() + 1);
            skdShoppingCartService.updateById(shoppingCart);
            return ReturnBody.success("增加成功");
        } else {
            return ReturnBody.error("购物车商品不存在");
        }
    }

    /**
     * #0060 减少购物车商品数量
     * @param shoppingCartId 购物车商品id
     * @return 返回是否减少成功信息
     */
    @ApiOperation("减少购物车商品数量")
    @PostMapping("reduce-quantity")
    public String reduceQuantity(@NotNull(message = "未提交购物车商品id") Long shoppingCartId) {
        // 减少购物车商品数量, 将quantity字段值每次减一
        SkdShoppingCart shoppingCart = skdShoppingCartService.getById(shoppingCartId);

        if (!Objects.isNull(shoppingCart)) {
            if (shoppingCart.getQuantity() > 1) {
                // 如果数量大于 1, 则减少数量
                shoppingCart.setQuantity(shoppingCart.getQuantity() - 1);
                skdShoppingCartService.updateById(shoppingCart);
                return ReturnBody.success("减少成功");
            } else {
                // 如果数量为 1, 则逻辑删除
                skdShoppingCartService.removeById(shoppingCartId);
                return ReturnBody.success("删除成功");
            }
        } else {
            return ReturnBody.error("购物车商品不存在");
        }
    }

    /**
     * #0061 获取下架商品列表
     * @return 返回下架的商品列表
     */
    @ApiOperation("获取下架商品列表")
    @GetMapping("off-shelves")
    public String getOffShelvesGoodsList() {
        return skdShoppingCartService.getOffShelvesGoodsList();
    }

    /**
     * #0062 清除用户购物车已失效商品
     * @return 返回是否清楚成功信息
     */
    @ApiOperation("清除购物车失效商品")
    @DeleteMapping("clear-goods")
    public String clearGoods() {
        Long usId;
        try {
            // 根据token获取用户id
            usId = tokenService.getUserIdFromToken();
        } catch (IllegalStateException e) {
            log.error(e.getMessage());
            return ReturnBody.error("token异常!");
        }
        // 查询用户购物车中已失效的商品id
        List<Long> invalidGoodsIds = skdShoppingCartService.getInvalidGoodsIds(usId);

        if (invalidGoodsIds.isEmpty()) {
            return ReturnBody.success("购物车中无失效商品");
        }

        // 逻辑删除用户购物车中已失效的商品
        skdShoppingCartService.removeByIds(invalidGoodsIds);

        return ReturnBody.success("清除成功!");
    }

    /**
     * #0063 删除购物车商品
     * @param shoppingCartId 购物车商品id
     * @return 返回是否删除成功信息
     */
    @ApiOperation("删除购物车商品")
    @DeleteMapping
    public String deleteShoppingCartGoods(@NotNull(message = "未提交购物车商品id") Long shoppingCartId) {
        // 根据购物车商品id逻辑删除对应的数据
        boolean removed = skdShoppingCartService.removeById(shoppingCartId);
        if (removed) {
            return ReturnBody.success("清除成功!");
        } else {
            return ReturnBody.error("购物车商品不存在");
        }
    }

    /**
     * #0064 查询购物车商品
     * @return 返回购物车商品列表
     */
    @ApiOperation("查看购物车商品")
    @GetMapping
    public String queryShoppingCartList(Long groupBuyingActivityId) {
        if(groupBuyingActivityId != null) {
            SkdGroupBuyingActivities res = skdGroupBuyingActivitiesService.getById(groupBuyingActivityId);
            if (Objects.isNull(res)) {
                return ReturnBody.error("该团购活动不存在或已删除!");
            }
        }
        return skdShoppingCartService.queryShoppingCartList(groupBuyingActivityId);
    }

}
